// pages/goods-indulgence/index.js
import {
    getNavigationbar,
} from "../../api/index.js"
Page({

    /**
     * 页面的初始数据
     */
    data: {
        navigateName: "",
        detailType: '', // 8=新品  9=特卖
        act: "",
        numGood: 0,
        isAshows: false,
        autoplay: false,
        isDots: false,
        scrollViewHeight: 0,
        swiperList: [],
        tabsList: [ ],
        active: 0,
        obj: {},
        goodsList: [],
        districtInfo: null,
        cartGoodDetail: {},
        isSpecs: false,
        isAhow: false,
        p: 1,
        banner_list: [],
        scatid: ''
    },
    onScrollViewScroll: function (e) {
        if (e.detail.scrollTop >= 600) { // 当滚动到200px位置时添加样式
            this.setData({
                isAshows: true,
                // scrollViewHeight:700
            });
        } else {
            this.setData({
                isAshows: false
            });
        }
        console.log('eeee',e.detail.scrollTop)
        // 在这里可以处理scroll-view滚动时的逻辑
    },
    nood() {
        console.log('点击', this.data.subdata)
        if (this.data.obj.shop_status == 0 || this.data.obj.shop_status == 1) {
            this.setData({
                isAhow: true,
            })
            return
        }
        wx.reLaunch({
            url: `/pages/goods_cart/index`,
        })

    },
    getShoppingCartList(item) {
        getNavigationbar({
            act: 'getcartsp',

        }).then(res => {
            console.log('购物车列表-一件代发', res)
            this.setData({
                numGood: res.numbers
            })

        });
    },
    /** 
     * 获取专区对应banner广告及分类数据回调函数
     */
    getRegion(type) {
        getNavigationbar({
            act: 'region_banner',
            type: type,
        }).then(res => {
            console.log('返回', res)
            if (res) {
                const swiperList = res.banner_list.map(item=>{
                    return {
                        ...item,
                        thumb: 'https://static.chonglaoban.cn' + item.thumb
                    }
                })
                this.data.tabsList = [{
                    id: "0",
                    name: "全部"
                },]
                res.cat_list.forEach(item => {
                    Object.assign(item,{
                        name:item.catname
                    })
                    this.data.tabsList.push(item)
                })
                this.data.scatid = this.data.tabsList[this.data.active].id
                console.log('=====',this.data.tabsList)
                this.setData({
                    swiperList,
                    isDots: swiperList.length > 1 ? true : false,
                    autoplay: swiperList.length > 1 ? true : false,
                    tabsList: this.data.tabsList,
                    scatid: this.data.scatid
                })
            }
        })
    },
    /**
     * 根据路由跳转名称获取对应商品列表
     * @param {*} name 
     */
    getGoodsList() {
        const {
            name,
            type
        } = this.data.params
        // 判断页面跳转执行对应数据请求
        switch (name) {
            case '9.8':
                this.getRegion(2) // 调用头部banner广告及分类请求
                this.getHttpGoodsList({
                    act: 'new_goods_region_list',
                    lack_inventory: 1,
                    scatid: this.data.scatid
                }, type)
                break;
            case '特卖专区':
                this.getRegion(1) // 调用头部banner广告及分类请求
                this.getHttpGoodsList({
                    act: 'special_sales_list',
                    scatid: this.data.scatid,
                    lack_inventory: 1,
                }, type)
                break;
            case '新品专区':
                this.getRegion(2) // 调用头部banner广告及分类请求
                this.getHttpGoodsList({
                    act: 'new_goods_region_list',
                    scatid: this.data.scatid,
                    lack_inventory: 1,
                }, type)
                break;
        }
    },

    /**
     * 执行请求获取数据回调方法数据
     */
    getHttpGoodsList(params, type) {
        console.log('switch', type)
        params.p = this.data.p
        getNavigationbar(params).then(res => {
            console.log(res)
            if (res) {
                res.forEach(element => {
                    if (this.data.obj.shop_status == 0 || this.data.obj.shop_status == 1) {
                        element.price = '***'
                        element.sellprice = '***'
                        element.marketprice = '***'
                    }
                });
                switch (params.act) {
                    case 'new_goods_region_list':
                        wx.nextTick(() => {
                            if (type) {
                                this.setData({
                                    goodsList: res,
                                })
                            } else {
                                this.setData({
                                    goodsList: this.data.goodsList.concat(res),
                                })
                            }
                        })
                        break;
                    case 'special_sales_list':
                        wx.nextTick(() => {
                            if (type) {
                                this.setData({
                                    goodsList: res,
                                })
                            } else {
                                this.setData({
                                    goodsList: this.data.goodsList.concat(res),
                                })
                            }
                        })
                        break;
                    case 'unconsume_region_list':
                        wx.nextTick(() => {
                            if (type) {
                                this.setData({
                                    goodsList: res,
                                })
                            } else {
                                this.setData({
                                    goodsList: this.data.goodsList.concat(res),
                                })
                            }
                        })
                        break;
                }
                this.computeScrollViewHeight() // 重新执行页面计算
            }
        })
    },
    /**
     * 计算页面可滚动区域的动态高度
     */
    computeScrollViewHeight() {
        let that = this
        let query = wx.createSelectorQuery().in(this)
        query.select('.header').boundingClientRect()
        query.exec(res => {
            let headerHeight = res[0].height;
            let winHeight; // 窗口可使用高度
            wx.getSystemInfo({
                success: (res) => {
                    winHeight = res.windowHeight
                }
            })
            let scrollHeight = winHeight - headerHeight; // 滚动区域高度
            that.setData({
                scrollViewHeight: scrollHeight
            })
        })
    },

    /**
     * 选项选择回调函数方法
     * @param {*} e 
     */
    handleTabs(e) {
        const {
            index
        } = e.currentTarget.dataset
        console.log(e)
        let tabId
        if (index != 0) {
            tabId = this.data.tabsList[index].id
        } else {
            tabId = ''
        }
        this.data.p = 1
        this.setData({
            active: index,
            scatid: tabId,
            goodsList: [],
            p:this.data.p
            // tabsList: [{
            //     id: "0",
            //     name: "全部"
            // }, ]
        })
        this.getGoodsList()

    },

    /**
     * 跳转商品详情页回调函数方法
     */
    handleGoodsDetail(e) {
        const {
            value
        } = e.currentTarget.dataset
        const params = {
            goodsId: value.id,
            detailType: this.data.detailType,
            act: this.data.act,
        }
        console.log('特卖',)
        wx.navigateTo({
            url: `/pages/goods_detail/index?data=${JSON.stringify(params)}`,
        })
    },
    /**
     * 选择商品规格加入购物车回调函数方法
     */
    handleJoinCart(e) {
        const {
            value
        } = e.currentTarget.dataset
        console.log("value", value)
        if (this.data.obj.shop_status == 0 || this.data.obj.shop_status == 1) {
            this.setData({
                isAhow: true,
            })
            return
        }
        let act = ''
        if (this.data.detailType == '8') {
            act = 'new_goods_region_detail'
        } else if (this.data.detailType == '9') {
            act = 'special_sales_detail'

        }
        console.log('this.data.detailType', this.data.detailType)
        getNavigationbar({
            act: act,
            id: value.id
        }).then(res => {
            console.log(res)
            this.setData({
                isSpecs: true,
                cartGoodDetail: res
            })
        })
    },
    /**
     * 完成规格选择将商品添加至购物车
     */
    handleConfirm(e) {
        console.log('传回', e)
        let data = e.detail
        this.setData({
            isSpecs: false
        })
        if (data.type == 0) { //普通
            console.log("this.data.detailType)", this.data.detailType)
            let act = ''
            if (this.data.detailType == '8') {
                act = 'new_goods_region_cart'
            } else if (this.data.detailType == '9') {
                act = 'special_price_cart'

            }
            getNavigationbar({
                act: act,
                id: data.commodityId || '',
                propertyid: data.data.propertyid || '',
                numbers: data.count || '',

            }).then(res => {
                console.log('添加编辑购物车-一件代发', res)
                this.getShoppingCartList()
                if (res.result == 101) {
                    wx.nextTick(() => {
                        getApp().Tips({
                            title: res.msg
                        })
                    })
                } else {
                    wx.nextTick(() => {
                        getApp().Tips({
                            title: "商品已添加至购物车"
                        })
                    })

                }
            });
        } else if (data.type == 1) { //一件代发
            ReDistributionMall({
                act: 'addEditMallCart',
                productid: data.commodityId || '',
                propertyid: data.data.productid || '',
                numbers: data.count || '',

            }).then(res => {
                console.log('添加编辑购物车-商品列表', res)
                if (res.result == 101) {
                    wx.nextTick(() => {
                        getApp().Tips({
                            title: res.msg
                        })
                    })
                } else {
                    wx.nextTick(() => {
                        getApp().Tips({
                            title: "商品已添加至购物车"
                        })
                    })
                    this.getShoppingCartList()
                    // wx.navigateTo({
                    //     url: `/pages/behalf-shopping-cart/index?data=${JSON.stringify(res)}`,
                    // })
                }

            });
        }

    },
    onShareAppMessage: function () {
        // let name 
        // if(this.data.navigateName == name) {
        //     name = name
        // } else if(this.data.navigateName == '新品专区') {
        //     name = '新品钜惠'
        // }
        return {
            title: this.data.navigateName,
            path: `/pages/goods_indulgence/index?data=${JSON.stringify(this.data.params)}`, // 分享的页面路径
            imageUrl: '', // 分享的图片链接
        }
    },
    onShareTimeline: function () {
        // let name 
        // if(this.data.navigateName == '特卖专区') {
        //     name = '好货特卖 '
        // } else if(this.data.navigateName == '新品专区') {
        //     name = '新品钜惠'
        // }
        return {
            title: this.data.navigateName,
            path: `/pages/goods_indulgence/index?data=${JSON.stringify( this.data.params)}`, // 分享的页面路径
            imageUrl: '', // 分享的图片链接
        }
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        if (options.data) {
            let params = JSON.parse(options.data)
            let type = params.value.type;
            let detailType
            let act
            let name = '商品列表';
            if (type == 7) {
                name = '新人专享';
                detailType = '7';
                act = 'unconsume_region_detail'
            } else
            if (type == 1 || type == 8) {
                name = '新品专区';
                detailType = '8';
                act = 'new_goods_region_detail'
            } else if (type == 2 || type == 9) {
                name = '特卖专区';
                detailType = '9';
                act = 'special_sales_detail'
            }
            this.data.act = act;
            this.setData({
                navigateName: name,
                detailType: detailType,
                params: params,
            }, () => {
                this.getGoodsList()
            })
        }
        let res = wx.getStorageSync('USER')
        console.log('setStorageSync', res)
        this.data.obj = res
        this.getShoppingCartList()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        this.computeScrollViewHeight()
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },



    handleScrollToLower() {
        console.log('触发到底部提示打印');
        this.data.p++
        getNavigationbar({
            act: 'coupon_pro_list',
            couponid: this.data.id,
            p: this.data.p
        }).then(res => {
            this.data.goodsList = this.data.goodsList.concat(res.pro_list)
            console.log('res.pro_list', res.pro_list)
            this.setData({
                goodsList: this.data.goodsList
            });
            console.log(this.data.goodsList)
        })
    },
    scrollToLower() {
        this.data.p++
        this.getGoodsList(this.data.param)
    },

})